package com.gym.emp.web.controller;

import com.github.pagehelper.PageInfo;
import com.gym.emp.common.Const;
import com.gym.emp.entity.Emp;
import com.gym.emp.services.EmpServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * @author ming
 * 2021/11/18
 * description:
 */
@Controller
@RequestMapping("/emp")
public class EmpController {

    @Autowired
    private EmpServices es;

    /**
     *@author:ming
     *@Date: 2021/11/19
     *@description:登录
     * @ResponseBody的作用其实是将java对象转为json格式的数据
     */
    @RequestMapping("/login")
    @ResponseBody
    public String loign(String ename, String pwd, HttpSession session) {
        Emp e = es.login(ename, pwd);
        if (e!=null) {
            //保存用户登录数据到session
            session.setAttribute(Const.IS_LOGIN, e);
            //清除之前的手机验证码
//            session.removeAttribute(Const.PHONE_CODE);
            //返回成功信息
            return Const.SUCCESS;
        }
        return Const.FAILE;
    }

    /**
     * @author:ming
     * @Date: 2021/11/19
     * @description:分页显示
     * @RequestParam(defaultValue = "1") 当他修饰的值为null的时候给默认值1
     * model.addAttribute("test", "张三");//把值放到Model里面  取值${test}
     */
    @RequestMapping("/index")
    public String getAll(Model model, @RequestParam(defaultValue = "1") Integer pageIndex) {
        PageInfo<Emp> empByPage = es.getEmpByPage(pageIndex);
        model.addAttribute("empInfo", empByPage);
        return "page";
    }

    /**
     * @author:ming
     * @Date: 2021/11/19
     * @description:删除
     * @RequestParam：将请求参数绑定到你控制器的方法参数上（是springmvc中接收普通参数的注解）
     */
    @RequestMapping("/delEmp")
    public String delEmp(@RequestParam(value = "eno", required = false) Integer eno) {
        //快捷键eno.nn
        if (eno != null) {
            es.delEmp(eno);
        }
        //重定向
        return "redirect:/emp/page";
    }

    /**
     * @author:ming
     * @Date: 2021/11/19
     * @description:添加
     */
    @RequestMapping("/addEmp")
    public String addEmp(Emp emp) {
        es.addEmp(emp);
        return "redirect:/emp/page";
    }

    /**
     * @author:ming
     * @Date: 2021/11/19
     * @description:eno查找用户信息
     *      * model.addAttribute("test", "张三");//把值放到Model里面  取值${test}
     */
    @RequestMapping("/getEmpByEno")
    public String getEmpByEno(Integer eno, Model model) {
        Emp e = es.getEmpByEno(eno);
        model.addAttribute("emp", e);
        return "update";
    }

    /**
     * @author:ming
     * @Date: 2021/11/19
     * @description:修改信息
     */
    @RequestMapping("/updateEmp")
    public String updateEmp(Emp emp) {
        es.updateEmp(emp);
        //重定向
        return "redirect:/emp/page";
    }




}
